package com.it.dao;

import com.it.domain.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * 对数据库进行操作
 */
public class StudentDao {

    // 模拟数据库
    private static ArrayList<Student> students = new ArrayList<>();

    // 添加student
    public boolean add(Student student) {
        return students.add(student);
    }

    // 判断id是否存在
    public boolean isExist(String id) {
        for (Student student : students) {
            if (student.getStudentId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    // 查询学生信息
    public List<Student> findAll() {
        return students;
    }

    // 删除学生信息
    public boolean delete(String id) {
        //判断id是否存在
        if (!isExist(id)) {
            return false;
        }
        for (Student student : students) {
            if (student.getStudentId().equals(id)) {
                students.remove(student);
                return true;
            }
        }
        return false;
    }

    //修改学生信息
    public boolean update(Student student) {
        //判断id是否存在
        if (!isExist(student.getStudentId())) {
            return false;
        }
        for (Student s : students) {
            if (s.getStudentId().equals(student.getStudentId())) {
                s.setName(student.getName());
                s.setAge(student.getAge());
                s.setGender(student.getGender());
                s.setMajor(student.getMajor());
                return true;
            }
        }
        return false;
    }

}
